import org.json.JSONObject;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/update_book")
public class UpdateBook extends HttpServlet {
    private static final long serialVersionUID = 1L;


    private static final String URL = "jdbc:mysql://localhost:3306/linux_exam";
    private static final String USER = "root";
    private static final String PASSWORD = "1qaz@WSX#EDC";

  
    public JSONObject updateBook(String bookNumber, String bookName, String bookAuthor) {
        try (

                Connection connection = DriverManager.getConnection(URL, USER, PASSWORD);

                PreparedStatement preparedStatement = connection.prepareStatement("UPDATE t_book SET name =?, author =? WHERE number =?");
        ) {
            preparedStatement.setString(1, bookName);
            preparedStatement.setString(2, bookAuthor);
            preparedStatement.setString(3, bookNumber);

            int rowsAffected = preparedStatement.executeUpdate();
            if (rowsAffected > 0) {

                try (
                        PreparedStatement selectStatement = connection.prepareStatement("SELECT * FROM t_book WHERE number =? AND name =? AND author =?");
                ) {
                    selectStatement.setString(1, bookNumber);
                    selectStatement.setString(2, bookName);
                    selectStatement.setString(3, bookAuthor);
                    try (ResultSet resultSet = selectStatement.executeQuery()) {
                        if (resultSet.next()) {
                            JSONObject updatedBook = new JSONObject();
                            updatedBook.put("number", resultSet.getString("number"));
                            updatedBook.put("name", resultSet.getString("name"));
                            updatedBook.put("author", resultSet.getString("author"));
                            return updatedBook;
                        }
                    }
                }
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");

        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();


        String bookNumber = request.getParameter("number");
        String bookName = request.getParameter("name");
        String bookAuthor = request.getParameter("author");


        if (bookNumber == null || bookNumber.isEmpty() || bookName == null || bookName.isEmpty() || bookAuthor == null || bookAuthor.isEmpty()) {
            JSONObject errorJson = new JSONObject();
            errorJson.put("error", "图书编号、名称和作者不能为空");
            out.println(errorJson.toString());
            return;
        }

        JSONObject resultJson = updateBook(bookNumber, bookName, bookAuthor);
        if (resultJson!= null) {
            out.println(resultJson.toString());
        } else {
            JSONObject errorJson = new JSONObject();
            errorJson.put("error", "图书更新失败");
            out.println(errorJson.toString());
        }
        out.close();
    }
}




























